package com.df.springmvc.system.manage.controller;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.common.bean.DhtmlGridBean;
import com.df.springmvc.common.bean.CurrentUserBean;
import com.df.springmvc.common.bean.MsgBean;
import com.df.springmvc.common.util.WebUtil;
import com.df.springmvc.supper.SupperController;
import com.df.springmvc.system.manage.services.IndexServices;


@Controller
@RequestMapping("/system/manage/Index")
/**
 * 本类仅做一些简单数据处理如:
 *格式验证
 *数据封装
 * @author David
 *
 */
public class IndexController extends SupperController{
	String page_model="system/manage/" + this.getClass().getSimpleName().replace("Controller","") + "_";
	@Autowired
	IndexServices services;
	@Autowired
	MessageSource messageSource;
	
	public IndexController(){
		// TODO Auto-generated constructor stub
	}
	@RequestMapping(value={"/getMenuJson"},method=RequestMethod.POST)
	public void getMenuJson(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		try{
			WebUtil.json(response,new MsgBean(services.getMenuJson(),true));
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}finally{
		}
	}
	@RequestMapping(value={"/"},method=RequestMethod.GET)
	public String login(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		
		//查询当前登录用户的权限信息
		
		return page_model;
	}
	@RequestMapping("/toPortlet")
	public String toPortlet(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		System.out.println(11);
		return page_model + "Portlet";
	}
	@RequestMapping(value={"/toPersonalInfo"})
	public String toPersonalInfo(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		try{
			//addRequestAttribute("map",services.view(getParamMap(request,"map").get("loginName")));
			addRequestAttribute("map",services.view(getParamMap(request,"map")));
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return page_model + "PersonalInfo";
	}
	@RequestMapping(value={"/toUpdPassword"})
	public String toUpdPassword(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		try{
			//addRequestAttribute("map",services.getMessageList(getParamMap(request,"map").get("loginName")));
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return page_model + "UpdPassword";
	}
	@RequestMapping(value={"/toMessage"})
	public String toMessage(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		try{
			//addRequestAttribute("map",services.getMessageList(getParamMap(request,"map").get("loginName")));
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return page_model + "Message";
	}
	@RequestMapping(value={"/doUpdPassword"})
	public void doUpdPassword(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		String oldPassword = getParamMap(request,"map").get("oldPassword");
		String currentPassword  = getCurrentUserBean().getLoginPassword();
		try{
			if(!oldPassword.equals(currentPassword)){
				throw new Exception("与当前登录密码不一致");
			}
			 Object rs=services.updPassword(getParamMap(request,"map"));
			 CurrentUserBean currentUserBean=new CurrentUserBean();
			 currentUserBean.setLoginPassword(getParamMap(request,"map").get("password"));
			 request.getSession().setAttribute("CurrentUserBean",currentUserBean);
			WebUtil.json(response,new MsgBean(rs));
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}
		
		//WebUtil.json(response,new MsgBean("OK"));
	}
	@RequestMapping(value={"/queryMessageNum"})
	public void queryMessageNum(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		try{
			
			@SuppressWarnings("unchecked")
			ArrayList<Object> list = (ArrayList<Object>) services.getMessageList(getParamMap(request,"map"));
			WebUtil.json(response,new MsgBean(list));
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@RequestMapping(value={"/queryMessage"})
	public void queryMessage(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		try{
			DhtmlGridBean gridBean=services.queryMessage(getParam_PagingBean(request),getParamMap(request,"map"));
			WebUtil.json(response,new MsgBean(gridBean));
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@RequestMapping(value={"/UpdMessage"})
	public void UpdMessage(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		try{
			HashMap<String, String> paramMap = getParamMap(request,"map"); 
			Object rs=services.updMessage(paramMap);
			WebUtil.json(response,new MsgBean(rs));
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//WebUtil.json(response,new MsgBean("OK"));
	}
	/**
	 * 修改信息状态
	 */
	@RequestMapping(value={"/" + doUpd})
	public void doUpd(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		try{
			HashMap<String, String> paramMap = getParamMap(request,"map"); 
			Object rs=services.upd(paramMap);
			WebUtil.json(response,new MsgBean(rs));
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//WebUtil.json(response,new MsgBean("OK"));
	}
}
